package fkti1381bkh;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.*;

public class VTree  extends JPanel{
	 
	 private JPanel LO;
	 private Paper maket;
	 private JTextField txt1;
	 private  JButton crt;
	 private JTextField txt2;
	 private  JButton chs;
	 private JTextField txt3;
	 private JButton add;
	 private JButton del;
	 private JButton gene;
	 private JButton toavl;
	 private JButton svtr;
	 private JTextArea txt4;
	 private JButton prttrs;
	 private JButton freell;
	 LinkedList<Root> avlll;
	 
	 VTree()  
	 {
		avlll=new LinkedList<Root>();
	 }
	/**
	 * Create GUI
	 * @return GUI
	 */
	 public JPanel createContentPane(){
		
		 LO=new JPanel();
		 LO.setLayout(null);
			 
	
		 maket=new Paper();
		 maket.setBounds(500, 50, 800, 600);
		 maket.setBackground(Color.WHITE);
		 maket.setVisible(true);
		 LO.add(maket);
		 

		 txt1=new JTextField("(1(2)(3))");
		 txt1.setLayout(null);
		 txt1.setBounds(20, 60, 200, 25);
		 txt1.setVisible(true);
		 LO.add(txt1);

		 crt=new JButton("Построить дерево");
		 crt.setLayout(null);
		 crt.setBounds(240, 60, 150, 20);
		 class Crt_Tree implements ActionListener {
			 private char in_str[];
			 public void actionPerformed(ActionEvent e){
				 in_str=txt1.getText().toCharArray();
				 BinTree U=new BinTree();
				 U.sethead(U.create(in_str, U.gethead()));
				 U.delete_empty(U.gethead());
				 maket.setVisible(false);
				 maket=new Paper();
				 maket.sethead(U.gethead());		 
				 maket.setBounds(500, 50, 800, 600);
				 maket.setBackground(Color.WHITE);
				 maket.setVisible(true);
				 LO.add(maket);
			 }
		 }
		 crt.addActionListener(new Crt_Tree());
		 LO.add(crt);

		 
		 txt2=new JTextField("Введите путь(l,r,s). Строка должна заканчиваться s.");
		 txt2.setLayout(null);
		 txt2.setBounds(20, 100, 320, 25);
		 txt2.setVisible(true);
		 LO.add(txt2);
		 
		 chs=new JButton("Выбрать");
		 chs.setLayout(null);
		 chs.setBounds(340, 100, 150, 20);
		 class Chs_Root implements ActionListener {
			 private char in_str[]; 
			 public void actionPerformed(ActionEvent e){
				 in_str=txt2.getText().toCharArray();
				 BinTree U=new BinTree();
				 U.sethead(maket.gethead());
				 
				 maket.setVisible(false);
				 maket=new Paper();
				 maket.sethead(U.gethead());
				 maket.setcur(U.chose_bt(U.gethead(),in_str));
				 maket.setBounds(500, 50, 800, 600);
				 maket.setBackground(Color.WHITE);
				 maket.setVisible(true);
				 LO.add(maket);
			 }
		 }
		 chs.addActionListener(new Chs_Root());
		 LO.add(chs);

		 
		 txt3=new JTextField("В какую сторону добавить(l,r)?");
		 txt3.setLayout(null);
		 txt3.setBounds(20, 150, 200, 25);
		 txt3.setVisible(true);
		 LO.add(txt3);
		 
		 add=new JButton("Добавить");
		 add.setLayout(null);
		 add.setBounds(240, 150, 150, 20);
		 class Add_Elem implements ActionListener {
			 private char in_str[]; 
			 public void actionPerformed(ActionEvent e) {
				 in_str=txt3.getText().toCharArray();
				 BinTree U=new BinTree();
				 U.sethead(maket.gethead());
				 U.setcurrent(maket.getcur());
				 U.add_elem(U.getcurrent(), in_str[0]);
				 
				 maket.setVisible(false);
				 maket=new Paper();
				 maket.sethead(U.gethead());
				 maket.setcur(U.getcurrent());
				 maket.setBounds(500, 50, 800, 600);;
				 maket.setBackground(Color.WHITE);
				 maket.setVisible(true);
				 LO.add(maket);
			 }
		 }
		 add.addActionListener(new Add_Elem());
		 LO.add(add);

		 
		 del=new JButton("Удалить");
		 del.setLayout(null);
		 del.setBounds(240, 175, 150, 20);
		 class Del_Elem implements ActionListener {
			 public void actionPerformed(ActionEvent e) {
				 BinTree U=new BinTree();
				 U.sethead(maket.gethead());
				 U.setcurrent(maket.getcur());
				 U.sethead(U.del_elem(U.gethead(),U.getcurrent()));
				 
				 maket.setVisible(false);
				 maket=new Paper();
				 maket.sethead(U.gethead());
				 maket.setBounds(500, 50, 800, 600);;
				 maket.setBackground(Color.WHITE);
				 maket.setVisible(true);
				 LO.add(maket); 
			 }
		 }
		 del.addActionListener(new Del_Elem());
		 LO.add(del);

		 
		 gene=new JButton("Сгенерировать дерево");
		 gene.setLayout(null);
		 gene.setBounds(20,200,400,50);
		 class Gen_Tree implements ActionListener {
			 public void actionPerformed(ActionEvent e) {
				 maket.setVisible(false);
				 BinTree U;
				 do
				 {
					 U=new BinTree();
					 U.sethead(U.generate(0));
					 U.check_balance(U.gethead());
				 }while(U.gethead()==null);
				 maket=new Paper();
				 maket.sethead(U.gethead());
				 maket.setBounds(500, 50, 800, 600);;
				 maket.setBackground(Color.WHITE);
				 maket.setVisible(true);
				 LO.add(maket); 	
			 }
		 }
		 gene.addActionListener(new Gen_Tree());
		 LO.add(gene);

		 
		 toavl=new JButton("Перестроить в AVL");
		 toavl.setLayout(null);
		 toavl.setBounds(20,270, 200,50);
		 class To_Avl implements ActionListener {
			 public void actionPerformed(ActionEvent e) {
				BinTree U=new BinTree(); 
				U.sethead(maket.gethead());
				U.setcurrent(maket.getcur());
				do
				{
					int sv=0;
					U.sethead(U.tree_change(U.gethead()));
					sv++;
					if(sv==100) break;
				}while(!U.check_avl(U.gethead()));
				 maket.setVisible(false);
				 maket=new Paper();
				 maket.sethead(U.gethead());
				 maket.setcur(U.getcurrent());
				 maket.setBounds(500, 50, 800, 600);;
				 maket.setBackground(Color.WHITE);
				 maket.setVisible(true);
				 LO.add(maket); 	
			 }	 
		 }
		 toavl.addActionListener(new To_Avl());
		 LO.add(toavl);

		 
		 svtr = new JButton("Сохранить дерево");
		 svtr.setLayout(null);
		 svtr.setBounds(250, 270, 200, 50);
		 class Save_Tree implements ActionListener {
			 public void actionPerformed(ActionEvent e) {
				 avlll.addLast(maket.gethead());
				
			 }
		 }
		 svtr.addActionListener(new Save_Tree());
		 LO.add(svtr);

		 
		 txt4=new JTextArea("");
		 txt4.setLayout(null);
		 txt4.setBounds(20, 340, 450, 270);
		 LO.add(txt4);
		 prttrs=new JButton("Показать сохраненные");
		 prttrs.setLayout(null);
		 prttrs.setBounds(20, 635, 200, 50);
		 class Print_List implements ActionListener {
			 public void actionPerformed(ActionEvent e) {
				txt4.setText("");
				Iterator<Root> iter=avlll.iterator();
				while(iter.hasNext())
				{
					BinTree U=new BinTree();	
					U.sethead(iter.next()); 
					String str=txt4.getText();
					txt4.setText(str+"\n"+U.getSerialization());
				}
			 }
		 }
		 prttrs.addActionListener(new Print_List());
		 LO.add(prttrs);		 

		 
		 freell=new JButton("Удалить сохранения");
		 freell.setLayout(null);
		 freell.setBounds(250, 635, 200, 50);
		 class Del_Saves implements ActionListener {
			 public void actionPerformed(ActionEvent e) {
				avlll.clear();
				
				txt4.setText("");
			 }
		 }
		 freell.addActionListener(new Del_Saves());
		 LO.add(freell);	 

		 return LO;
	 }
}
